var React = require('react');

var HotPostList = React.createClass({
  getInitialState: function () {
    return {
      resultList: []
    };
  },

  componentDidMount: function () {
    $.getJSON(baseUrl + "getSelfTextList", function (result, status) {
      console.log(result.data);
      if (this.isMounted()) {
        this.setState({
          resultList: result.data
        });
      }
    }.bind(this));
  },

  handleDeletePost: function (id) {
    $.getJSON(baseUrl + "delTextById/" + id, function (result, status) {
      console.log(result);
      if (this.isMounted()) {
        this.setState({
          resultList: result.data
        });
      }
    }.bind(this));
  },

  render: function () {
    return (
      <div className="manage-post">
        <div className="devide-title">
          <h1>所有文章</h1>
        </div>
        <ul className="post-list">
          {this.state.resultList.map(function (post) {
          var postHref = 'post.html?id=' + post.id;
          var editHref = 'edit-post.html?id=' + post.id;
          return (
              <li key={post.id}>
                <a href={postHref} target="_blank">{post.title}</a>
                <div className="manage-action">
                  <a href={editHref} target="_blank">编辑</a>
                  <a href="#" onClick={this.handleDeletePost.bind(this, post.id)}>删除</a>
                </div>
                <div>
                  <span className="post-type">{post.text_type}</span>
                  <span className="post-time">发表时间：{post.update_date}</span>
                </div>
              </li>
          )
        }, this)}
        </ul>
      </div>
    );
  }
});

module.exports = HotPostList;
